package net.sf.xoperator.ui.txt;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.validator.UrlValidator;

import net.sf.xoperator.blackboard.NoPlanBlackBoardControl;
import net.sf.xoperator.store.RdfStoreInfo;
import net.sf.xoperator.ui.StringMessage;

public class AddDatastoreCommand extends Command {

	@Override
	public String getHelp() {
		return "adds a datastore to the configuration of the client. use: add ds <name> <uri>";
	}

	@Override
	public String getKeyWord() {
		return "add ds";
	}

	@Override
	public List<Object> performInternal(String call,
			NoPlanBlackBoardControl control, Interactor interactor) {
		String[] splittedcall = call.split(" ");
		if (splittedcall.length != 4) {
			return Arrays.asList(new Object[] { new StringMessage(
					"Wrong parameter count. help: " + this.getHelp()) });
		}
		UrlValidator validator = new UrlValidator(new String[] { "http",
				"https" });
		if (!validator.isValid(splittedcall[3])) {
			return Arrays.asList(new Object[] { new StringMessage(
					"Please check the syntax of your URI") });
		}

		RdfStoreInfo newstore = new RdfStoreInfo();
		newstore.setName(splittedcall[2]);
		newstore.setEndpointUri(splittedcall[3]);

		control.propagateConfiguration(newstore);

		return Arrays.asList(new Object[] { new StringMessage("store added") });

	}

	@Override
	public Pattern getValidationPattern() {

		return Pattern.compile(".*");
	}

}
